﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WoodManager.Header;
using WarehouseManagement.Header;
using Common;

namespace WoodManager.BO
{
    class DinhMucVatTuSonBO
    {
        public String getVan(SqlConnection conn, ref DataTable tbVan)
        {
            String cmd = "Select distinct " + WoodManagerHeader.THVS_ID_VAN + ", "
                + WoodManagerHeader.THVS_TEN_VAN
                + " From " + WoodManagerHeader.VW_TO_HOP_VAN_SON;
            CommonDO cmDO = new CommonDO();
            return  cmDO.getTable(conn, ref tbVan, cmd);
        }

        public String LoadDataDinhMucSon(SqlConnection conn, string IdToHopVanSon ,ref DataTable tbDinhMucSon)
        {
            String cmd = "SELECT * from VwDinhMucSon where IDToHopSon = N'"+ IdToHopVanSon +"'";
            CommonDO cmDO = new CommonDO();
            return cmDO.getTable(conn, ref tbDinhMucSon, cmd);
        }

        public String getSon(SqlConnection conn, ref DataTable tbSon, String sIDVan)
        {
            String cmd = "Select * "
                + " From " + WoodManagerHeader.VW_TO_HOP_VAN_SON
                + " where " + WoodManagerHeader.THVS_ID_VAN + " = '"+ sIDVan +"'";
            CommonDO cmDO = new CommonDO();
            return cmDO.getTable(conn, ref tbSon, cmd);
        }

        public String getNhomVT(SqlConnection conn, ref DataTable tbNhomVT)
        {
            String cmd = "Select * "
                + " From " + WarehouseHeader.TB_NHOM_VT_89;
            CommonDO cmDO = new CommonDO();
            return cmDO.getTable(conn, ref tbNhomVT, cmd);
        }

        public String getVatTu(SqlConnection conn, ref DataTable tbVatTu, String sIdVT89)
        {
            String cmd = "Select VTID, TenVatTu "
                + " From " + WarehouseHeader.VW_VAT_TU
                + " Where ID89 = N'" + sIdVT89 + "'";               
            CommonDO cmDO = new CommonDO();
            return cmDO.getTable(conn, ref tbVatTu, cmd);
        }

        public String InsertRecord(SqlConnection conn, StructDinhMucVatTuSon stDMS)
        {
            String cmd = "INSERT INTO " + WoodManagerHeader.TB_DINH_MUC_SON + " (";
            cmd = string.Concat(cmd,WoodManagerHeader.DMS_ID_TO_HOP_SON, ",");
            cmd = string.Concat(cmd,WoodManagerHeader.DMS_MA_VAT_TU_SON, ",");
            cmd = string.Concat(cmd, WoodManagerHeader.DMS_DINH_LUONG, ",");
            cmd = string.Concat(cmd, WoodManagerHeader.DMS_GHI_CHU);

            cmd = string.Concat(cmd, ") VALUES (");

            cmd = string.Concat(cmd,"N'", stDMS.sID, "',");
            cmd = string.Concat(cmd, "N'", stDMS.sVatTuSon, "',");
            cmd = string.Concat(cmd, "N'", stDMS.sDinhLuong, "',");
            cmd = string.Concat(cmd, "N'", stDMS.sGhiChu, "')");

            CommonDO cmDO = new CommonDO();
            return cmDO.ExcecuteQuery(conn, cmd);
        }

        public String UpdateRecord(SqlConnection conn, ref StructToHopVan stTHVS)
        {
            String cmd = "Update ToHopVanSon SET ";
            cmd = cmd + "IDVan = N'" + stTHVS.sIDVan + "', ";
            cmd = cmd + "IDSon = N'" + stTHVS.sIDSon + "', ";
            cmd = cmd + "GhiChu = N'" + stTHVS.sGhiChu + "'";
            cmd = cmd + "WHERE IDToHopSon = N'" + stTHVS.sIDTopHopVanSon + "'";

            CommonDO cmDO = new CommonDO();
            String ret = cmDO.ExcecuteQuery(conn, cmd);
            if (ret == CommonHeader.NO_ERROR)
            {
                stTHVS.sIDTopHopVanSon = stTHVS.sIDVan + stTHVS.sIDSon;
            }
            return ret;
        }

        public String DeleteRecord(SqlConnection conn, ref StructToHopVan stTHVS)
        {
            String cmd = "DELETE ToHopVanSon WHERE IDToHopSon = N'" + stTHVS.sIDTopHopVanSon + "'";

            CommonDO cmDO = new CommonDO();
            String ret = cmDO.ExcecuteQuery(conn, cmd);
            if (ret == CommonHeader.NO_ERROR)
            {
                stTHVS.sIDTopHopVanSon = "";
                stTHVS.sIDSon = "";
                stTHVS.sIDVan = "";
                stTHVS.sGhiChu = "";
            }
            return ret;
        }

    }
}
